﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Media; 
using System.Windows.Media.Media3D;

namespace SharpSoft.WPF.D3D
{
    /// <summary>
    /// http://www.pudn.com/Download/item/id/2601007.html
    /// </summary>
    public class Text3D : Geometry2DTo3D
    {
        public Text3D()
        {

        }

        #region 文本属性

        public string Text
        {
            get { return (string)GetValue(TextProperty); }
            set { SetValue(TextProperty, value); }
        }

        public static readonly DependencyProperty TextProperty =
            DependencyProperty.Register("Text", typeof(string), typeof(Text3D), new PropertyMetadata(null));



        public FontFamily FontFamily
        {
            get { return (FontFamily)GetValue(FontFamilyProperty); }
            set { SetValue(FontFamilyProperty, value); }
        }

        public static readonly DependencyProperty FontFamilyProperty =
            DependencyProperty.Register("FontFamily", typeof(FontFamily), typeof(Text3D), new PropertyMetadata(SystemFonts.MessageFontFamily));



        public FontWeight FontWeight
        {
            get { return (FontWeight)GetValue(FontWeightProperty); }
            set { SetValue(FontWeightProperty, value); }
        }

        public static readonly DependencyProperty FontWeightProperty =
            DependencyProperty.Register("FontWeight", typeof(FontWeight), typeof(Text3D), new PropertyMetadata(SystemFonts.MessageFontWeight));



        public FontStyle FontStyle
        {
            get { return (FontStyle)GetValue(FontStyleProperty); }
            set { SetValue(FontStyleProperty, value); }
        }

        public static readonly DependencyProperty FontStyleProperty =
            DependencyProperty.Register("FontStyle", typeof(FontStyle), typeof(Text3D), new PropertyMetadata(SystemFonts.MessageFontStyle));



        public FontStretch FontStretch
        {
            get { return (FontStretch)GetValue(FontStretchProperty); }
            set { SetValue(FontStretchProperty, value); }
        }

        public static readonly DependencyProperty FontStretchProperty =
            DependencyProperty.Register("FontStretch", typeof(FontStretch), typeof(Text3D), new PropertyMetadata(FontStretches.Normal));



        public double FontSize
        {
            get { return (double)GetValue(FontSizeProperty); }
            set { SetValue(FontSizeProperty, value); }
        }

        public static readonly DependencyProperty FontSizeProperty =
            DependencyProperty.Register("FontSize", typeof(double), typeof(Text3D), new PropertyMetadata(SystemFonts.MessageFontSize));



        public FlowDirection FlowDirection
        {
            get { return (FlowDirection)GetValue(FlowDirectionProperty); }
            set { SetValue(FlowDirectionProperty, value); }
        }

        public static readonly DependencyProperty FlowDirectionProperty =
            DependencyProperty.Register("FlowDirection", typeof(FlowDirection), typeof(Text3D), new PropertyMetadata(FlowDirection.LeftToRight));



        public Brush Foreground
        {
            get { return (Brush)GetValue(ForegroundProperty); }
            set { SetValue(ForegroundProperty, value); }
        }

        public static readonly DependencyProperty ForegroundProperty =
            DependencyProperty.Register("Foreground", typeof(Brush), typeof(Text3D), new PropertyMetadata(Brushes.Black));



        protected override void OnPropertyChanged(DependencyPropertyChangedEventArgs e)
        {
            base.OnPropertyChanged(e);
            if (e.Property == TextProperty || e.Property == ForegroundProperty || e.Property == FontFamilyProperty || e.Property == FontSizeProperty || e.Property == FontWeightProperty || e.Property == FontStretchProperty || e.Property == FontStyleProperty || e.Property == FlowDirectionProperty)
            {
                this.CreateGeometry();
            }
        }
        #endregion

        private void CreateGeometry()
        {
            if (string.IsNullOrEmpty(this.Text))
            {
                this.Geometry = null;
                return;
            }

            FormattedText formattedText = new FormattedText(this.Text, System.Globalization.CultureInfo.CurrentUICulture, this.FlowDirection, new Typeface(this.FontFamily, this.FontStyle, this.FontWeight, this.FontStretch), this.FontSize, this.Foreground);
            var g = formattedText.BuildGeometry(new Point());
            this.Geometry = g;
        }
    }
}
